package com.cn.wkditui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/19.
 */
public class Auth2Activity extends BaseActivity {
    @BindView(R.id.etname)
    EditText etname;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.et_tel)
    EditText etTel;
    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.et_commet)
    EditText etCommet;
    @BindView(R.id.bt_regist)
    Button btRegist;
    @BindView(R.id.bt_before)
    Button btBefore;
    @BindView(R.id.bt_noreigst)
    Button btNoreigst;
    @BindView(R.id.bt_no)
    Button btNo;
    String url = Consts.HOST+Consts.ADDSUVER;
    private String id;
    private boolean isEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_a2);
        ButterKnife.bind(this);
        init();
//        load();

    }

    private void init() {
        id  = getIntent().getStringExtra("id");
        if(getIntent().hasExtra("act")){
            isEdit = true;

            load();
        }else{
            isEdit = false;
        }
        Log.e("Auth1", "是否编辑:" + isEdit + "id===========" + id);
    }


    private void load() {
        RequestParams params = new RequestParams();
        Log.e("id", "ud----------------:" + id);
        params.addBodyParameter("id",id);
        params.addBodyParameter("type", "3");
        new HttpUtils().send(HttpRequest.HttpMethod.POST, Consts.HOST + Consts.GETSURVER, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("Auth2", responseInfo.result);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);
                    if (json.getInt("code") == 1) {
                        etTel.setText(json.getJSONObject("data").getString("tel"));
                        etPhone.setText(json.getJSONObject("data").getString("phone"));
                        etname.setText(json.getJSONObject("data").getString("contacts"));
                        etCommet.setText(json.getJSONObject("data").getString("detailInfoComment"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }

    @OnClick({R.id.bt_regist, R.id.bt_before,R.id.bt_noreigst, R.id.bt_no})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_regist:
                ll1.setBackground(getResources().getDrawable(R.drawable.item_grey));
                ll2.setBackground(getResources().getDrawable(R.drawable.item_grey));
                ll3.setBackground(getResources().getDrawable(R.drawable.item_grey));
                if(TextUtils.isEmpty(etname.getText().toString())){
                    ll1.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etname.setHintTextColor(getResources().getColor(R.color.red));
                }else  if(TextUtils.isEmpty(etTel.getText().toString())){
                    ll2.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etTel.setHintTextColor(getResources().getColor(R.color.red));
                }else if(TextUtils.isEmpty(etPhone.getText().toString())){
                    ll3.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etPhone.setHintTextColor(getResources().getColor(R.color.red));
                }else{

//                    new HttpUtils()
                    RequestParams params = new RequestParams();

                    params.addBodyParameter("staffPhone",MyPreference.getInstance(Auth2Activity.this).getStaffid());
                    params.addBodyParameter("id", id);
                    Log.e("id", "id=====" + id);
                    params.addBodyParameter("tel", etTel.getText().toString());
                    params.addBodyParameter("phone",etPhone.getText().toString());
                    params.addBodyParameter("contacts",etname.getText().toString());
                    params.addBodyParameter("id",getIntent().getStringExtra("id"));
                    params.addBodyParameter("status", "1");
                    params.addBodyParameter("detailInfoComment", etCommet.getText().toString());
//                    params.addBodyParameter("staffId",etbh.getText().toString());
//									System.out.println("id"+MyPreference.getInstance(AuthActivity.this).getUser_Id());
//                    System.out.println("address"+name+etstrnum.getText().toString());
//                    System.out.println("staffId"+etbh.getText().toString());

                    HttpUtils httpUtils = new HttpUtils();
                    httpUtils.send(HttpRequest.HttpMethod.POST, url,params, new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            System.out.println("arg1"+arg1);
                            Toast.makeText(getBaseContext(), "网络异常", 0).show();
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            System.out.println("arg0."+arg0.result);
                            try {
                                JSONObject json = new JSONObject(arg0.result);
                                int code = json.getInt("code");
                                if(code==1){
                                    Toast.makeText(getBaseContext(), "注册成功", 0).show();
                                    Intent intent = new Intent(Auth2Activity.this, RigsterSuccessActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                }else{
                                    Toast.makeText(getBaseContext(), json.getString("message"), 0).show();
                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    });
                }

                break;
            case R.id.bt_before:
                Auth2Activity.this.finish();
                break;
            case R.id.bt_noreigst:
                ll1.setBackground(getResources().getDrawable(R.drawable.item_grey));
                ll2.setBackground(getResources().getDrawable(R.drawable.item_grey));
                if(TextUtils.isEmpty(etname.getText().toString())){
                    ll1.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etname.setHintTextColor(getResources().getColor(R.color.red));
                }else  if(TextUtils.isEmpty(etTel.getText().toString())){
                    ll2.setBackground(getResources().getDrawable(R.drawable.item_red));
                    etTel.setHintTextColor(getResources().getColor(R.color.red));
                }else{

                    noRegister();
                }


                break;
            case R.id.bt_no:
                Utils.ShowDialog(Auth2Activity.this, Consts.TITLE, new Utils.CallBack() {
                    @Override
                    public void onclickConfirm(Dialog dialog) {
                        noPeihe(dialog);
                    }

                    @Override
                    public void onclickCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                });
                        break;
        }
    }

    private void noPeihe(final Dialog dialog) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("id",id);
        params.addBodyParameter("staffPhone", MyPreference.getInstance(Auth2Activity.this).getStaffid());
        params.addBodyParameter("status", "-2");
        params.addBodyParameter("detailInfoComment", etCommet.getText().toString());
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, url,params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                dialog.dismiss();
                System.out.println("arg1" + arg1);
                Toast.makeText(getBaseContext(), "网络异常", 0).show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                dialog.dismiss();
                System.out.println("arg0." + arg0.result);
                try {
                    JSONObject json = new JSONObject(arg0.result);
                    int code = json.getInt("code");
                    if (code == 1) {
                        Toast.makeText(getBaseContext(), "保存成功", 0).show();
//                                    MyPreference.getInstance(Auth1Activity.this).setToken(json.getJSONObject("data").getString("token"));
                        Intent intent = new Intent(Auth2Activity.this, MainActivity.class);
                        intent.putExtra("id", json.getString("data"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getBaseContext(), json.getString("message"), 0).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
    }

    private void noRegister() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("staffPhone", MyPreference.getInstance(Auth2Activity.this).getStaffid());
        params.addBodyParameter("tel", etTel.getText().toString());
        params.addBodyParameter("phone", etPhone.getText().toString());
        params.addBodyParameter("contacts", etname.getText().toString());
        params.addBodyParameter("id", id);
        params.addBodyParameter("status", "2");
        params.addBodyParameter("detailInfoComment", etCommet.getText().toString());
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                System.out.println("arg1" + arg1);
                Toast.makeText(getBaseContext(), "网络异常", 0).show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {

                System.out.println("arg0." + arg0.result);
                try {
                    JSONObject json = new JSONObject(arg0.result);
                    int code = json.getInt("code");
                    if (code == 1) {
                        Toast.makeText(getBaseContext(), "完成", 0).show();
                        Intent intent = new Intent(Auth2Activity.this, RigsterSuccessActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getBaseContext(), json.getString("message"), 0).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return  true;
    }
}
